<?php

defined('SYSPATH') or die('No direct script access.');

require_once('application/include/idna_convert.class.php');

class Model_Api extends Model {

    public $clientauth;
    public $paramsauth;
    public $loginresult;

    public function __construct($paramsauth) {
        $this->paramsauth = $paramsauth;
        try {
            $this->loginresult = $this->login();
        } catch (SoapFault $e) {
            echo $e->faultstring;
        }
    }

    public function login() {
        $this->clientauth = new SoapClient(
                        "http://www.rookee.ru/api/authorize.asmx?WSDL",
                        array('soap_version' => SOAP_1_2)
        );
        $auth = $this->clientauth->Login($this->paramsauth);
        if (!$auth->LoginResult)
            throw new SoapFault("Server", "Вы не авторизованы на rookee.ru");
        return $auth->LoginResult;
    }

    public function getProject($project_id) {
        if ($this->loginresult) {
            $client = new SoapClient(
                            "http://www.Rookee.ru/api/RookeeApi.asmx?WSDL",
                            array('soap_version' => SOAP_1_2)
            );
            $client->__setCookie("ASP.NET_SessionId", $this->clientauth->_cookies["ASP.NET_SessionId"][0]);
            $values = Array(
                "projectID" => $project_id
            );
            $result = $client->GetProject($values);           
            return $result->GetProjectResult;
        }
    }
    
    public function getProjects() {
        $idn = new idna_convert(array('idn_version' => 2008));
        $projects = array();
        if ($this->loginresult) {
            $client = new SoapClient(
                            "http://www.Rookee.ru/api/RookeeApi.asmx?WSDL",
                            array('soap_version' => SOAP_1_2)
            );
            $client->__setCookie("ASP.NET_SessionId", $this->clientauth->_cookies["ASP.NET_SessionId"][0]);

            $result = $client->GetProjects();
            foreach ($result->GetProjectsResult->ProjectShortInfo as $key => $project) {
                $projects[$key]['id'] = $project->ID;
                $projects[$key]['domain'] = (stripos($project->Domain, "xn--") !== false) ? $idn->decode($project->Domain) : $project->Domain;
            }
            return $projects;
        } else {
            return false;
        }
    }

    public function getProjectRegion($project_id) {
        if ($this->loginresult) {
            $client = new SoapClient(
                            "http://www.Rookee.ru/api/RookeeApi.asmx?WSDL",
                            array('soap_version' => SOAP_1_2)
            );
            $client->__setCookie("ASP.NET_SessionId", $this->clientauth->_cookies["ASP.NET_SessionId"][0]);
            $values = Array(
                "projectID" => $project_id
            );
                      
            $result = $client->GetProject($values);           
            return $result->GetProjectResult->RegionID;
        }
    }

    public function getQueries($project_id) {
        $queries = array();
        if ($this->loginresult) {
            $client = new SoapClient(
                            "http://www.Rookee.ru/api/RookeeApi.asmx?WSDL",
                            array('soap_version' => SOAP_1_2)
            );
            $client->__setCookie("ASP.NET_SessionId", $this->clientauth->_cookies["ASP.NET_SessionId"][0]);
            $values = Array(
                "projectID" => $project_id
            );
            try {
                $result = $client->GetQueries($values);
                if (!isset($result->GetQueriesResult->QueryInfo))
                    throw new SoapFault("Server", "Нет информации о проекте с таким ID");
                foreach ($result->GetQueriesResult->QueryInfo as $key => $query) {
                    $queries[$key]['id'] = $query->ID;
                    $queries[$key]['title'] = $query->Title;
                    $queries[$key]['target'] = $query->Target;
                }
                return $queries;
            } catch (SoapFault $e) {
                echo $e->faultstring;
            }
        }
    }

    public function getPositions($query_id, $date_from, $date_to) {
        if ($this->loginresult) {
            $client = new SoapClient(
                            "http://www.Rookee.ru/api/RookeeApi.asmx?WSDL",
                            array('soap_version' => SOAP_1_2)
            );
            $client->__setCookie("ASP.NET_SessionId", $this->clientauth->_cookies["ASP.NET_SessionId"][0]);
            $values = Array(
                "queryID" => $query_id,
                "dateFrom" => $date_from,
                "dateTo" => $date_to
            );
            try {
                $result = $client->GetPositions($values);    
                return $result->GetPositionsResult->AdvertPosition;
            } catch (SoapFault $e) {
                echo $e->faultstring;
            }
        }
    }
    
    public function GetQueriesInfo($project_id) {
        if ($this->loginresult) {
            $client = new SoapClient(
                            "http://www.Rookee.ru/api/RookeeApi.asmx?WSDL",
                            array('soap_version' => SOAP_1_2)
            );
            $client->__setCookie("ASP.NET_SessionId", $this->clientauth->_cookies["ASP.NET_SessionId"][0]);
            $values = Array(
                "projectID" => $project_id
            );
            $result = $client->GetQueriesInfo($values);
            return $result;
        }
        
    }
    
    public function getRegions() {
        if ($this->loginresult) {
            $client = new SoapClient(
                            "http://www.Rookee.ru/api/RookeeApi.asmx?WSDL",
                            array('soap_version' => SOAP_1_2)
            );
            $client->__setCookie("ASP.NET_SessionId", $this->clientauth->_cookies["ASP.NET_SessionId"][0]);
            $result = $client->GetRegions();
            return $result->GetRegionsResult->RegionInfo;
        }
        
    }

}

?>
